<?php

// 修改合作机构密码
// PUT org.updatePwd

namespace app\api\resource\org;
use think\Db;
use think\Loader;
use think\Session;

class UpdatePwd
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['orgLogin'];
    // 描述
    public $_description = '修改合作机构密码';
    // 参数
    public $_param  = [
        'passport' => '旧登录密码',
        'password' => '登录密码',
        'confpwd'  => '确认密码',
    ];
    // 返回
    public $_return = [
        201 => '编辑成功',
        400 => '旧登录密码不正确',
        422 => '请求参数不合法',
        500 => '服务器异常',
    ];

    public function run(&$request)
    {
        $params = $request->put();

        $validate = Loader::validate('Agency');
        if ( !$validate->scene('updatePwd')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $orgId  = Session::get('org.id');
        $ret = Db::name('agency')->where('id',$orgId)->where('password',$params['passport'])->find();
        if ( is_null($ret) ) return [400,'旧登录密码不正确'];

        $num = Db::name('agency')->where('id',$orgId)->update(['password'=>$params['password']]);

        return ($num >= 0) ? [ 201,[] ] : 500;
    }
}
